
package de.justphil.tcg.tcgandroidclient.gui;

import de.justphil.tcg.tcgandroidclient.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class CardAttribute extends RelativeLayout {
	
	private View content;
	private final static int ACTUAL = 0;
	private final static int BASE = 1;
	private final static int NUM_FIELDS = 10;
	private ImageView[][] fields;
	
	public CardAttribute(Context context, AttributeSet attrs) {
	
		super(context, attrs);
		LayoutInflater inflater =
				(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		content = inflater.inflate(R.layout.card_attribute, this);
		init(attrs);
	}
	
	public CardAttribute(Context context, AttributeSet attrs, int defstyle) {
	
		super(context, attrs, defstyle);
		init(attrs);
	}
	
	private void init(AttributeSet attrs) {
	
		if (!this.isInEditMode()) {
			TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CardAttribute);
			int color = a.getColor(R.styleable.CardAttribute_color, Color.BLACK);
			int baseValue = a.getInt(R.styleable.CardAttribute_base_value, 0);
			int value = a.getInt(R.styleable.CardAttribute_value, 0);
			a.recycle();
			/* Reading the fields */
			{
				fields = new ImageView[2][NUM_FIELDS];
				fields[ACTUAL][0] = (ImageView) content.findViewById(R.id.img_actual_1);
				fields[ACTUAL][1] = (ImageView) content.findViewById(R.id.img_actual_2);
				fields[ACTUAL][2] = (ImageView) content.findViewById(R.id.img_actual_3);
				fields[ACTUAL][3] = (ImageView) content.findViewById(R.id.img_actual_4);
				fields[ACTUAL][4] = (ImageView) content.findViewById(R.id.img_actual_5);
				fields[ACTUAL][5] = (ImageView) content.findViewById(R.id.img_actual_6);
				fields[ACTUAL][6] = (ImageView) content.findViewById(R.id.img_actual_7);
				fields[ACTUAL][7] = (ImageView) content.findViewById(R.id.img_actual_8);
				fields[ACTUAL][8] = (ImageView) content.findViewById(R.id.img_actual_9);
				fields[ACTUAL][9] = (ImageView) content.findViewById(R.id.img_actual_10);
				fields[BASE][0] = (ImageView) content.findViewById(R.id.img_base_1);
				fields[BASE][1] = (ImageView) content.findViewById(R.id.img_base_2);
				fields[BASE][2] = (ImageView) content.findViewById(R.id.img_base_3);
				fields[BASE][3] = (ImageView) content.findViewById(R.id.img_base_4);
				fields[BASE][4] = (ImageView) content.findViewById(R.id.img_base_5);
				fields[BASE][5] = (ImageView) content.findViewById(R.id.img_base_6);
				fields[BASE][6] = (ImageView) content.findViewById(R.id.img_base_7);
				fields[BASE][7] = (ImageView) content.findViewById(R.id.img_base_8);
				fields[BASE][8] = (ImageView) content.findViewById(R.id.img_base_9);
				fields[BASE][9] = (ImageView) content.findViewById(R.id.img_base_10);
			}
			setValue(value);
			setBaseValue(baseValue);
			setColor(color);
		}
	}
	
	private void setValue(int value, int type) {
	
		for (int i = 0; i < NUM_FIELDS; i++) {
			if (i < value) {
				fields[type][i].setVisibility(VISIBLE);
			}
			else {
				fields[type][i].setVisibility(INVISIBLE);
			}
		}
	}
	
	public void setColor(int color) {
	
		for (int i = 0; i < NUM_FIELDS; i++) {
			fields[ACTUAL][i].setBackgroundColor(color);
		}
	}
	
	public void setBaseValue(int value) {
	
		setValue(value, BASE);
	}
	
	public void setValue(int value) {
	
		setValue(value, ACTUAL);
	}
}
